function [value,flag,tree]=retreat_tree_qpsk(s_hat,tree,i,nt)

theta=[-3*pi/4 3*pi/4 -pi/4 pi/4];
s_try=exp(j*theta);
b=qpsk2bin(s_hat(i));
index=2*b(1)+b(2)+1;
tree(i,index)=0;
alternatives=find(tree(i,:)==1);
if(length(alternatives)>1)
	flag=1;
	value=s_try(alternatives(ceil(rand*length(alternatives))));
elseif(length(alternatives)>0)
	flag=1;
	value=s_try(alternatives);
else
	if(i~=nt)
		flag=0;
		value=0;
	else
		tree(i,index)=1;
		value=s_hat(i);
		flag=1;
	end
end
